﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Election.SettlementClaimantPreliminaryDecision
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Election
{
  public class SettlementClaimantPreliminaryDecision : KingdomDecision
  {
    [SaveableField(400)]
    public readonly Settlement Settlement;
    [SaveableField(401)]
    private Clan _ownerClan;

    internal static void AutoGeneratedStaticCollectObjectsSettlementClaimantPreliminaryDecision(
      object o,
      List<object> collectedObjects)
    {
      ((KingdomDecision) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Settlement);
      collectedObjects.Add((object) this._ownerClan);
    }

    internal static object AutoGeneratedGetMemberValueSettlement(object o)
    {
      return (object) ((SettlementClaimantPreliminaryDecision) o).Settlement;
    }

    internal static object AutoGeneratedGetMemberValue_ownerClan(object o)
    {
      return (object) ((SettlementClaimantPreliminaryDecision) o)._ownerClan;
    }

    public SettlementClaimantPreliminaryDecision(Clan proposerClan, Settlement settlement)
      : base(proposerClan)
    {
      this.Settlement = settlement;
      this._ownerClan = settlement.OwnerClan;
    }

    public override bool IsAllowed()
    {
      return Campaign.Current.Models.KingdomDecisionPermissionModel.IsAnnexationDecisionAllowed(this.Settlement);
    }

    public override int GetProposalInfluenceCost()
    {
      return Campaign.Current.Models.DiplomacyModel.GetInfluenceCostOfAnnexation(this.ProposerClan);
    }

    public override TextObject GetGeneralTitle()
    {
      TextObject generalTitle = new TextObject("{=XFr4IfXf} Change Owner of {SETTLEMENT}");
      generalTitle.SetTextVariable("SETTLEMENT", this.Settlement.Name);
      return generalTitle;
    }

    public override TextObject GetSupportTitle()
    {
      TextObject supportTitle = new TextObject("{=a48xlNUb}Should the owner of {SETTLEMENT} change?");
      supportTitle.SetTextVariable("SETTLEMENT", this.Settlement.Name);
      return supportTitle;
    }

    public override TextObject GetChooseTitle()
    {
      TextObject chooseTitle = new TextObject("{=a48xlNUb}Should the owner of {SETTLEMENT} change?");
      chooseTitle.SetTextVariable("SETTLEMENT", this.Settlement.Name);
      return chooseTitle;
    }

    public override TextObject GetSupportDescription()
    {
      TextObject supportDescription = new TextObject("{=hBJbnoDn}{FACTION_LEADER} will decide if the owner of {SETTLEMENT} will change.");
      supportDescription.SetTextVariable("FACTION_LEADER", this.DetermineChooser().Leader.Name);
      supportDescription.SetTextVariable("SETTLEMENT", this.Settlement.Name);
      return supportDescription;
    }

    public override TextObject GetChooseDescription()
    {
      TextObject chooseDescription = new TextObject("{=JHR4ySCf}As {?IS_FEMALE}queen{?}king{\\?} you must decide if the owner of {SETTLEMENT} should change.");
      chooseDescription.SetTextVariable("IS_FEMALE", this.DetermineChooser().Leader.IsFemale ? 1 : 0);
      chooseDescription.SetTextVariable("SETTLEMENT", this.Settlement.Name);
      return chooseDescription;
    }

    public override float CalculateMeritOfOutcome(DecisionOutcome candidateOutcome)
    {
      float meritOfOutcome = 0.0f;
      float num = 50f;
      List<Clan> clanList = new List<Clan>();
      if (this._ownerClan.Kingdom != null)
        clanList.AddRange((IEnumerable<Clan>) this._ownerClan.Kingdom.Clans.ToList<Clan>());
      else
        clanList.Add(this._ownerClan);
      foreach (Clan clan in clanList)
      {
        int relation = clan.Leader.GetRelation(this._ownerClan.Leader);
        bool settlementOwnerChange = ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) candidateOutcome).ShouldSettlementOwnerChange;
        if (!settlementOwnerChange && relation > 0 || settlementOwnerChange && relation <= 0)
          meritOfOutcome += num * (float) MathF.Abs(relation);
      }
      return meritOfOutcome;
    }

    public override IEnumerable<DecisionOutcome> DetermineInitialCandidates()
    {
      yield return (DecisionOutcome) new SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome(true);
      yield return (DecisionOutcome) new SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome(false);
    }

    public override Clan DetermineChooser() => ((Kingdom) this.Settlement.MapFaction).RulingClan;

    protected override bool ShouldBeCancelledInternal()
    {
      return this.Settlement.MapFaction != this.Kingdom;
    }

    public float CalculateSupport(Clan clan)
    {
      return this.DetermineSupport(clan, (DecisionOutcome) new SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome(true));
    }

    public override float DetermineSupport(Clan clan, DecisionOutcome possibleOutcome)
    {
      bool settlementOwnerChange = ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) possibleOutcome).ShouldSettlementOwnerChange;
      int num1 = clan.GetRelationWithClan(this._ownerClan);
      if (clan == this._ownerClan)
        num1 = 300;
      float num2 = 20f;
      float num3 = this.Settlement.OwnerClan != clan ? num2 + (float) num1 * 0.7f + this.Settlement.OwnerClan.TotalStrength * 0.01f : num2 * 20f;
      float support = 0.0f;
      if (settlementOwnerChange)
        support = -num3;
      else if (!settlementOwnerChange)
        support = num3;
      return support;
    }

    public override void DetermineSponsors(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      foreach (DecisionOutcome possibleOutcome in (List<DecisionOutcome>) possibleOutcomes)
      {
        if (((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) possibleOutcome).ShouldSettlementOwnerChange)
          possibleOutcome.SetSponsor(this.ProposerClan);
        else
          this.AssignDefaultSponsor(possibleOutcome);
      }
    }

    protected override int GetInfluenceCostOfSupportInternal(Supporter.SupportWeights supportWeight)
    {
      if (this.Settlement.IsTown)
      {
        switch (supportWeight)
        {
          case Supporter.SupportWeights.SlightlyFavor:
            return 20;
          case Supporter.SupportWeights.StronglyFavor:
            return 60;
          case Supporter.SupportWeights.FullyPush:
            return 200;
          default:
            return 0;
        }
      }
      else
      {
        switch (supportWeight)
        {
          case Supporter.SupportWeights.SlightlyFavor:
            return 15;
          case Supporter.SupportWeights.StronglyFavor:
            return 50;
          case Supporter.SupportWeights.FullyPush:
            return 150;
          default:
            return 0;
        }
      }
    }

    public override void ApplyChosenOutcome(DecisionOutcome chosenOutcome)
    {
      if (!((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) chosenOutcome).ShouldSettlementOwnerChange)
        return;
      SettlementClaimantDecision claimantDecision = new SettlementClaimantDecision(this.ProposerClan, this.Settlement, (Hero) null, this._ownerClan);
      claimantDecision.IsEnforced = true;
      this.ProposerClan.Kingdom.AddDecision((KingdomDecision) claimantDecision, true);
    }

    public override TextObject GetSecondaryEffects()
    {
      return new TextObject("{=CsZfuPOe}Voting against or in favor of the current owner of the settlement will affect your relation with that clan accordingly.");
    }

    public override void ApplySecondaryEffects(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes,
      DecisionOutcome chosenOutcome)
    {
      foreach (DecisionOutcome possibleOutcome in (List<DecisionOutcome>) possibleOutcomes)
      {
        DecisionOutcome decisionOutcome;
        bool settlementOwnerChange = ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) (decisionOutcome = possibleOutcome)).ShouldSettlementOwnerChange;
        foreach (Supporter supporter in decisionOutcome.SupporterList)
        {
          if (supporter.Clan.Leader != this._ownerClan.Leader)
            ChangeRelationAction.ApplyRelationChangeBetweenHeroes(supporter.Clan.Leader, this._ownerClan.Leader, Campaign.Current.Models.DiplomacyModel.GetRelationChangeAfterVotingInSettlementOwnerPreliminaryDecision(supporter.Clan.Leader, settlementOwnerChange));
        }
      }
    }

    public override TextObject GetChosenOutcomeText(
      DecisionOutcome chosenOutcome,
      KingdomDecision.SupportStatus supportStatus,
      bool isShortVersion = false)
    {
      TextObject empty = TextObject.Empty;
      TextObject parent;
      if (((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) chosenOutcome).ShouldSettlementOwnerChange)
      {
        switch (supportStatus)
        {
          case KingdomDecision.SupportStatus.Majority:
            parent = new TextObject("{=Zo65bOpH}{RULER.NAME} has decided to give {SETTLEMENT} to a new clan with the support of with {?RULER.GENDER}her{?}his{\\?} council.");
            break;
          case KingdomDecision.SupportStatus.Minority:
            parent = new TextObject("{=w3sfcpoa}{RULER.NAME} has decided to give {SETTLEMENT} to a new clan despite the opposition of {?RULER.GENDER}her{?}his{\\?} council");
            break;
          default:
            parent = new TextObject("{=JzALLCEX}{RULER.NAME} has decided to give {SETTLEMENT} to a new clan, with {?RULER.GENDER}her{?}his{\\?} council evenly split on the matter.");
            break;
        }
      }
      else
      {
        switch (supportStatus)
        {
          case KingdomDecision.SupportStatus.Majority:
            parent = new TextObject("{=vkeHpUEB}{RULER.NAME} has decided against giving {SETTLEMENT} to a new clan with the support of with {?RULER.GENDER}her{?}his{\\?} council.");
            break;
          case KingdomDecision.SupportStatus.Minority:
            parent = new TextObject("{=9Cbeagow}{RULER.NAME} has decided against giving {SETTLEMENT} to a new clan over the objections of {?RULER.GENDER}her{?}his{\\?} council.");
            break;
          default:
            parent = new TextObject("{=fP8NHthR}{RULER.NAME} has decided against giving {SETTLEMENT} to a new clan, with {?RULER.GENDER}her{?}his{\\?} council evenly split on the matter.");
            break;
        }
      }
      parent.SetTextVariable("SETTLEMENT", this.Settlement.Name);
      parent.SetTextVariable("KINGDOM", this.Settlement.MapFaction.InformalName);
      StringHelpers.SetCharacterProperties("RULER", this.Settlement.MapFaction.Leader.CharacterObject, parent);
      return parent;
    }

    public override DecisionOutcome GetQueriedDecisionOutcome(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      return possibleOutcomes.FirstOrDefault<DecisionOutcome>((Func<DecisionOutcome, bool>) (t => ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) t).ShouldSettlementOwnerChange));
    }

    public override KingdomDecision GetFollowUpDecision()
    {
      return this.Kingdom.UnresolvedDecisions.FirstOrDefault<KingdomDecision>((Func<KingdomDecision, bool>) (x => x is SettlementClaimantDecision claimantDecision && claimantDecision.Settlement == this.Settlement));
    }

    public class SettlementClaimantPreliminaryOutcome : DecisionOutcome
    {
      [SaveableField(400)]
      public bool ShouldSettlementOwnerChange;

      internal static void AutoGeneratedStaticCollectObjectsSettlementClaimantPreliminaryOutcome(
        object o,
        List<object> collectedObjects)
      {
        ((DecisionOutcome) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
      {
        base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      internal static object AutoGeneratedGetMemberValueShouldSettlementOwnerChange(object o)
      {
        return (object) ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome) o).ShouldSettlementOwnerChange;
      }

      public override TextObject GetDecisionTitle()
      {
        TextObject decisionTitle = new TextObject("{=kakxnaN5}{?SUPPORT}Yes{?}No{\\?}");
        decisionTitle.SetTextVariable("SUPPORT", this.ShouldSettlementOwnerChange ? 1 : 0);
        return decisionTitle;
      }

      public override TextObject GetDecisionDescription()
      {
        return this.ShouldSettlementOwnerChange ? new TextObject("{=1bbsq6uw}We should award this fief to a new clan") : new TextObject("{=uBcEUdxu}Ownership of the fief should remain as it is");
      }

      public override string GetDecisionLink() => (string) null;

      public override ImageIdentifier GetDecisionImageIdentifier() => (ImageIdentifier) null;

      public SettlementClaimantPreliminaryOutcome(bool shouldSettlementOwnerChange)
      {
        this.ShouldSettlementOwnerChange = shouldSettlementOwnerChange;
      }
    }
  }
}
